package com.ruoyi.business.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.business.domain.TUserChannel;
import com.ruoyi.business.mapper.TUserChannelMapper;
import com.ruoyi.business.service.ITUserChannelService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 用户通道Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-26
 */
@Service
public class TUserChannelServiceImpl implements ITUserChannelService 
{
    @Autowired
    private TUserChannelMapper tUserChannelMapper;

    /**
     * 查询用户通道
     * 
     * @param id 用户通道主键
     * @return 用户通道
     */
    @Override
    public TUserChannel selectTUserChannelById(Long id)
    {
        return tUserChannelMapper.selectTUserChannelById(id);
    }

    /**
     * 查询用户通道列表
     * 
     * @param tUserChannel 用户通道
     * @return 用户通道
     */
    @Override
    @DataScope(userAlias = "t")
    public List<TUserChannel> selectTUserChannelList(TUserChannel tUserChannel)
    {
        return tUserChannelMapper.selectTUserChannelList(tUserChannel);
    }

    @Override
    public List<TUserChannel> selectTUserChannelListByNoAuth(TUserChannel tUserChannel) {
        return tUserChannelMapper.selectTUserChannelListByNoAuth(tUserChannel);

    }


    /**
     * 新增用户通道
     * 
     * @param tUserChannel 用户通道
     * @return 结果
     */
    @Override
    public int insertTUserChannel(TUserChannel tUserChannel)
    {
        tUserChannel.setCreateTime(DateUtils.getNowDate());
        long userId = ShiroUtils.getUserId();
//        boolean superAdmin = ShiroUtils.getSysUser().isAdmin();
//        if(!superAdmin){
//            //是否已经添加过了
//            TUserChannel qryRes = getUserChannel(tUserChannel);
//            if(qryRes!=null){
//                throw new ServiceException(UserChannelExceptionEnum.RESULT_EXIST.getMessage());
//            }
//            //不能为自己添加通道
//            if(tUserChannel.getUserId() == userId){
//                throw new ServiceException(UserChannelExceptionEnum.NOT_SELF.getMessage());
//            }
//            //判断该商户是否有权限为他人添加通道
//            TUserChannel qry = new TUserChannel();
//            qry.setChannel(tUserChannel.getChannel());
//            qry.setUserId(ShiroUtils.getUserId());
//            qryRes = getUserChannel(qry);
//            if(qryRes==null){
//                throw new ServiceException(UserChannelExceptionEnum.NOT_PERS.getMessage());
//            }
//            if(qryRes.getUserRate()>tUserChannel.getUserRate()){
//                throw new ServiceException(UserChannelExceptionEnum.RATE_INVALID.getMessage());
//            }
//        }
        TUserChannel userChannel = new TUserChannel();
        userChannel.setUserId(tUserChannel.getUserId());
        userChannel.setChannel(tUserChannel.getChannel());
        List<TUserChannel> userChannelList = tUserChannelMapper.selectTUserChannelList(userChannel);
        if(userChannelList.size()==0){
            tUserChannelMapper.insertTUserChannel(tUserChannel);
        }
        return 0;
    }

    /**
     * 修改用户通道
     * 
     * @param tUserChannel 用户通道
     * @return 结果
     */
    @Override
    public int updateTUserChannel(TUserChannel tUserChannel)
    {
        tUserChannel.setUpdateTime(DateUtils.getNowDate());
        return tUserChannelMapper.updateTUserChannel(tUserChannel);
    }

    /**
     * 批量删除用户通道
     * 
     * @param ids 需要删除的用户通道主键
     * @return 结果
     */
    @Override
    public int deleteTUserChannelByIds(String ids)
    {
        return tUserChannelMapper.deleteTUserChannelByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户通道信息
     * 
     * @param id 用户通道主键
     * @return 结果
     */
    @Override
    public int deleteTUserChannelById(Long id)
    {
        return tUserChannelMapper.deleteTUserChannelById(id);
    }

    @Override
    public TUserChannel getUserChannel(TUserChannel userChannelParam) {
        TUserChannel query = new TUserChannel();
        if (ObjectUtil.isNotEmpty(userChannelParam.getUserId())) {
            query.setUserId(userChannelParam.getUserId());
        }
        if (ObjectUtil.isNotEmpty(userChannelParam.getChannel())) {
            query.setChannel(userChannelParam.getChannel());
        }
        List<TUserChannel> tUserChannels = tUserChannelMapper.selectTUserChannelList(query);
        if(tUserChannels.size()==0){
            return null;
        }else{
            return tUserChannels.get(0);
        }
    }

    @Override
    public List<TUserChannel> selectTPayChannelRatioCfglist(TUserChannel userChannelParam) {
        return Collections.emptyList();
    }
}
